﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.Alipay;

namespace StoreWeb.ajax
{
    public partial class storeTop : System.Web.UI.Page
    {
        //BLL.Role rolebll = new BLL.Role();
        Maticsoft.BLL.StoreTop storeTopbll = new Maticsoft.BLL.StoreTop();
        BLL.DealersGrade dealersGradebll = new BLL.DealersGrade();
        BLL.DealersManage userdealers = new BLL.DealersManage();
        BLL.StoreCheckAccount soreCheckAccountbll = new BLL.StoreCheckAccount();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int row = 0;
                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "delete")
                {
                    Response.Write((storeTopbll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                }
                //else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "confirm")
                //{

                //    Model.DealersManage ne = dealersManagebll.GetModel(Convert.ToInt32(Request["id"]));
                //    ne.userstate = "已审核";
                //    ne.audituser = Convert.ToInt32(Request.Cookies["UserLogin"].Values[0]);
                //    ne.audittime = DateTime.Now; ;
                //    row = dealersManagebll.Update(ne) ? 2 : 0;
                //    Response.Write(row);
                //}
                else
                {
                    Maticsoft.Model.StoreTop ne = new Maticsoft.Model.StoreTop();
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        ne = storeTopbll.GetModel(Convert.ToInt32(Request["id"]));

                        row = storeTopbll.Update(ne) ? 2 : 0;
                    }
                    else
                    {
                        ne.dealersid = Request["dealersid"];
                        ne.moneytop = Convert.ToDecimal(Request["moneytop"]);

                        //ne.isvip = Request["isvip"] == "RadioButton1" ? "0" : "1";
                        if (Request["waytop"] == "rad1")
                        {
                            ne.waytop = "POS机";
                        }
                        else if (Request["waytop"] == "rad2")
                        {
                            ne.waytop = "支付宝";
                            ne.createtime = DateTime.Now;
                            ne.statetop = "充值中";
                            ne.createuser = Convert.ToInt32(Request.Cookies["UserLogin"].Values[0]);
                            int returnid = storeTopbll.Add(ne);

                            ////////////////////////////////////////////请求参数////////////////////////////////////////////

                            //商户订单号，商户网站订单系统中唯一订单号，必填
                            string out_trade_no = "00000000" + returnid;

                            //订单名称，必填
                            string subject = "门店充值";

                            //付款金额，必填
                            string total_fee = Convert.ToDecimal(Request["moneytop"]).ToString("f2");
                            //string total_fee = "0.01".ToString();

                            //商品描述，可空
                            string body = "";
                            ////////////////////////////////////////////////////////////////////////////////////////////////

                            //把请求参数打包成数组
                            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
                            sParaTemp.Add("service", Com.Alipay.Config.service);
                            sParaTemp.Add("partner", Com.Alipay.Config.partner);
                            sParaTemp.Add("seller_id", Com.Alipay.Config.seller_id);
                            sParaTemp.Add("_input_charset", Com.Alipay.Config.input_charset.ToLower());
                            sParaTemp.Add("payment_type", Com.Alipay.Config.payment_type);
                            sParaTemp.Add("notify_url", Com.Alipay.Config.notify_url_top);
                            sParaTemp.Add("return_url", Com.Alipay.Config.return_url_top);
                            sParaTemp.Add("anti_phishing_key", Com.Alipay.Config.anti_phishing_key);
                            sParaTemp.Add("exter_invoke_ip", Com.Alipay.Config.exter_invoke_ip);
                            sParaTemp.Add("out_trade_no", out_trade_no);
                            sParaTemp.Add("subject", subject);
                            sParaTemp.Add("total_fee", total_fee);
                            sParaTemp.Add("body", body);
                            sParaTemp.Add("extra_common_param", Request.Cookies["UserLogin"].Values[0] + "|" + returnid);

                            //建立请求
                            string sHtmlText = Submit.BuildRequest(sParaTemp, "get", "确认");
                            Response.Write(sHtmlText);
                        }
                        else if (Request["waytop"] == "rad3")
                        {
                            ne.waytop = "微信";
                        }else
                        {
                            ne.waytop = "现金";
                            ne.createtime = DateTime.Now;
                            ne.statetop = "充值成功";
                            ne.createuser = Convert.ToInt32(Request.Cookies["UserLogin"].Values[0]);
                            int returnid = storeTopbll.Add(ne);
                            row = returnid > 0 ? 1 : 0;


                            Model.DealersManage ne2 = userdealers.GetModel(Convert.ToInt32(Request["dealersid"]));
                            //增加对账单
                            //Model.StoreCheckAccount ne3 = new Model.StoreCheckAccount();
                            //ne3.dealersid = Request["dealersid"];
                            //ne3.createtime = DateTime.Now;
                            //ne3.description = Request["moneytop"];
                            //ne3.oldmoney = ne2.moneybalance;
                            //ne3.changemoney = 0;
                            //ne3.newmoney = Convert.ToDecimal(Request["moneytop"]) + ne2.moneybalance;
                            //ne3.type = "门店充值";
                            //ne3.createuser = Convert.ToInt32(Request.Cookies["UserLogin"].Values[0]);
                            //ne3.typeid = returnid;
                            //soreCheckAccountbll.Add(ne3);
                            //增加门店余额
                            ne2.moneybalance = Convert.ToDecimal(Request["moneytop"]) + ne2.moneybalance;
                            userdealers.Update(ne2);
                        }

                        //ne.createtime = DateTime.Now;
                        //ne.statetop = "充值成功";
                        //ne.createuser = Convert.ToInt32(Request.Cookies["UserLogin"].Values[0]);
                        //int returnid = storeTopbll.Add(ne);
                        //row = returnid > 0 ? 1 : 0;


                        //Model.DealersManage ne2 = userdealers.GetModel(Convert.ToInt32(Request["dealersid"]));
                        ////增加对账单
                        //Model.StoreCheckAccount ne3 = new Model.StoreCheckAccount();
                        //ne3.dealersid = Request["dealersid"];
                        //ne3.createtime = DateTime.Now;
                        //ne3.description = Request["moneytop"];
                        //ne3.oldmoney = ne2.moneybalance;
                        //ne3.changemoney = 0;
                        //ne3.newmoney = Convert.ToDecimal(Request["moneytop"]) + ne2.moneybalance;
                        //ne3.type = "门店充值";
                        //ne3.createuser = Convert.ToInt32(Request.Cookies["UserLogin"].Values[0]);
                        //ne3.typeid = returnid;
                        //soreCheckAccountbll.Add(ne3);
                        ////增加门店余额
                        //ne2.moneybalance = Convert.ToDecimal(Request["moneytop"]) + ne2.moneybalance;
                        //userdealers.Update(ne2);

                    }
                    if (row == 1)
                    {
                        Response.Write("1");
                    }
                    else if (row == 2)
                    {
                        Response.Write("2");
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }
            }
        }
    }
}